package com.gqj.meter.service.impl;

import com.gqj.common.exception.ServiceException;
import com.gqj.common.utils.DateUtils;
import com.gqj.common.utils.StringUtils;
import com.gqj.common.utils.bean.BeanValidators;
import com.gqj.common.utils.uuid.HiultraUtil;
import com.gqj.meter.controller.MdOutTaskDetailsController;
import com.gqj.meter.domain.*;
import com.gqj.meter.mapper.*;
import com.gqj.meter.service.IMdOutTaskDetailsService;
import com.gqj.soapclient.client;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.xml.sax.SAXException;

import javax.validation.Validator;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 出入库明细Service业务层处理
 *
 * @author lxq
 * @date lxq-02-10
 */
@Service
public class MdOutTaskDetailsServiceImpl implements IMdOutTaskDetailsService {
    @Autowired
    private MdOutTaskDetailsMapper mdOutTaskDetailsMapper;
    @Autowired
    private MdMeterMapper mdMeterMapper;
    @Autowired
    private MdItMapper mdItMapper;
    @Autowired
    private MdLcEquipMapper mdLcEquipMapper;
    @Autowired
    private MdConcentratorMapper mdConcentratorMapper;
    @Autowired
    private MdComModuleMapper mdComModuleMapper;
    @Autowired
    private MdOtherMaterialMapper mdOtherMaterialMapper;
    @Autowired
    private MdOutTaskMapper mdOutTaskMapper;
    @Autowired
    protected Validator validator;
    private static final Logger log = LoggerFactory.getLogger(MdOutTaskDetailsServiceImpl.class);

//    @Autowired
//    private MdIoDetMapper mdIoDetMapper;
    @Autowired
    private TransactionTemplate transactionTemplate;

    /**
     * 查询出入库明细
     *
     * @param outTaskDetailsId 出入库明细主键
     * @return 出入库明细
     */
    @Override
    public MdOutTaskDetails selectMdOutTaskDetailsByOutTaskDetailsId(Long outTaskDetailsId) {
        return mdOutTaskDetailsMapper.selectMdOutTaskDetailsByOutTaskDetailsId(outTaskDetailsId);
    }

    /**
     * 查询出入库明细列表
     *
     * @param mdOutTaskDetails 出入库明细
     * @return 出入库明细
     */
    @Override
    public List<MdOutTaskDetails> selectMdOutTaskDetailsList(MdOutTaskDetails mdOutTaskDetails) {
        return mdOutTaskDetailsMapper.selectMdOutTaskDetailsList(mdOutTaskDetails);
    }

    /**
     * 导出出入库明细列表
     *
     * @param mdOutTaskDetails 出入库明细
     * @return 出入库明细
     */
    @Override
    public List<MdOutTaskDetails> exportMdOutTaskDetailsListExcel(MdOutTaskDetails mdOutTaskDetails) {
        return mdOutTaskDetailsMapper.exportMdOutTaskDetailsListExcel(mdOutTaskDetails);
    }

    /**
     * 新增出入库明细
     *
     * @param mdOutTaskDetailsList 出入库明细
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMdOutTaskDetails(List<MdOutTaskDetails> mdOutTaskDetailsList) {
        MdOutTaskDetails mdOutTaskDetailsMain = mdOutTaskDetailsList.get(0);
        MdOutTask mdOutTask = mdOutTaskMapper.selectMdOutTaskByOutTaskNo(mdOutTaskDetailsMain.getOutTaskNo());
        if (null==mdOutTask) {
            throw new ServiceException("任务单据'" + mdOutTaskDetailsMain.getOutTaskNo() + "'不存在");
        }
//        if (!"0".equals(mdOutTask.getStatusCode())) {
//            throw new ServiceException("任务不是待执行状态，不允许新增明细！");
//        }

        for (MdOutTaskDetails mdOutTaskDetails : mdOutTaskDetailsList) {
            this.updateServiceStatus(mdOutTask.getAssetsCategory(), mdOutTaskDetails.getAssetsId(), "02");
        }

//        int outNumber = mdOutTask.getOutNumber();
//        int importNumber = mdOutTaskDetailsList.size();

        MdOutTaskDetails mdOutTaskDetailsQueryCount = new MdOutTaskDetails();
        mdOutTaskDetailsQueryCount.setOutTaskNo(mdOutTask.getOutTaskNo());
//        int detailsNumber = mdOutTaskDetailsMapper.selectMdOutTaskDetailsCount(mdOutTaskDetailsQueryCount);

//        int total = importNumber + detailsNumber;
//        if (outNumber < total) {
//            throw new ServiceException("任务单据'" + mdOutTask.getOutTaskNo() + "'规定数量小于导入数量和已导入数量之和，无法导入");
//        }

        for (MdOutTaskDetails mdOutTaskDetails : mdOutTaskDetailsList) {
//            MdOutTaskDetails mdOutTaskDetailsQuery = new MdOutTaskDetails();
//            mdOutTaskDetailsQuery.setRfidNo(mdOutTaskDetails.getRfidNo());
//            MdOutTaskDetails mdOutTaskDetailsTemp = mdOutTaskDetailsMapper.selectCarryMdOutTaskDetailsCount(mdOutTaskDetailsQuery);
//            if (StringUtils.isNotNull(mdOutTaskDetailsTemp)) {
//                if (mdOutTaskDetailsTemp.getRepeatNumber() > 0) {
//                    throw new ServiceException("条形码 " + mdOutTaskDetails.getRfidNo() + " 的信息在" + mdOutTaskDetailsTemp.getOutTaskNo() + "的任务中已存在");
//                }
//            }

            mdOutTaskDetailsMapper.insertMdOutTaskDetails(mdOutTaskDetails);
        }
        return 1;
    }

    /**
     * 修改出入库明细
     *
     * @param mdOutTaskMap 出入库明细
     * @return 结果
     */
    @Override
    public int updateMdOutTaskDetails(MdOutTaskDetailsController.MdOutTaskMap mdOutTaskMap) throws IOException, ParserConfigurationException, SAXException {
        final String[] IotaskNo = {""};
        transactionTemplate.execute(new TransactionCallback<Void>() {
            @SneakyThrows
            public Void doInTransaction(TransactionStatus status) {
                try {
                    String assetsCategory = mdOutTaskMap.getAssetsCategory();

                    String useEquipment = mdOutTaskMap.getUseEquipment();

                    List<MdOutTaskDetails> mdOutTaskDetailsList = mdOutTaskMap.getMdOutTaskDetailsList();
                    for (MdOutTaskDetails mdOutTaskDetails : mdOutTaskDetailsList) {
                        MdOutTaskDetails mdOutTaskDetailsUpdate = new MdOutTaskDetails();
                        mdOutTaskDetailsUpdate.setIsComplete("1");
                        mdOutTaskDetailsUpdate.setOperationDate(DateUtils.getNowDate());
                        mdOutTaskDetailsUpdate.setIsNormal("01");
                        mdOutTaskDetailsUpdate.setUseEquipment(useEquipment);
                        if (null != mdOutTaskDetails.getOutTaskDetailsId()) {
                            mdOutTaskDetailsUpdate.setOutTaskDetailsId(mdOutTaskDetails.getOutTaskDetailsId());
                            mdOutTaskDetailsMapper.updateMdOutTaskDetails(mdOutTaskDetailsUpdate);
                        } else {
                            mdOutTaskDetailsUpdate.setOutTaskNo(mdOutTaskMap.getOutTaskNo());
                            mdOutTaskDetailsUpdate.setAssetsId(mdOutTaskDetails.getAssetsId());
                            mdOutTaskDetailsUpdate.setRfidNo(mdOutTaskDetails.getRfidNo());
                            mdOutTaskDetailsUpdate.setSortCode(mdOutTaskDetails.getSortCode());
                            mdOutTaskDetailsUpdate.setTypeCode(mdOutTaskDetails.getTypeCode());
                            mdOutTaskDetailsUpdate.setEquipId(mdOutTaskDetails.getEquipId());
                            mdOutTaskDetailsUpdate.setOperId(mdOutTaskMap.getOperId());
                            mdOutTaskDetailsUpdate.setWhId(mdOutTaskDetails.getWhId());
                            mdOutTaskDetailsUpdate.setWhAreaId(mdOutTaskDetails.getWhAreaId());
                            mdOutTaskDetailsUpdate.setStoreAreaId(mdOutTaskDetails.getStoreAreaId());
                            mdOutTaskDetailsUpdate.setStoreLocId(mdOutTaskDetails.getStoreLocId());

                            mdOutTaskDetailsMapper.insertMdOutTaskDetails(mdOutTaskDetailsUpdate);
                        }

                        switch (assetsCategory) {
                            case "01":
                                //删除台账
                                mdMeterMapper.deleteMdMeterByMeterId(mdOutTaskDetails.getAssetsId());
                                break;
                            case "02":
                                mdItMapper.deleteMdItByItId(mdOutTaskDetails.getAssetsId());
                                break;
                            case "03":
                                mdLcEquipMapper.deleteMdLcEquipByLcEquipId(mdOutTaskDetails.getAssetsId());
                                break;
                            case "04":
                                mdConcentratorMapper.deleteMdConcentratorByConcentratorId(mdOutTaskDetails.getAssetsId());
                                break;
                            case "05":
                                mdComModuleMapper.deleteMdComModuleByModuleId(mdOutTaskDetails.getAssetsId());
                                break;
                            case "10":
                                mdOtherMaterialMapper.deleteMdOtherMaterialByOtherMaterialId(mdOutTaskDetails.getAssetsId());
                                break;
                        }
                    }
                    MdOutTaskDetails mdOutTaskDetailsQuery = new MdOutTaskDetails();
                    mdOutTaskDetailsQuery.setOutTaskNo(mdOutTaskMap.getOutTaskNo());
                    mdOutTaskDetailsQuery.setIsComplete("0");
                    int count = mdOutTaskDetailsMapper.selectMdOutTaskDetailsCount(mdOutTaskDetailsQuery);
                    if (count == 0) {
                        MdOutTask mdOutTask = new MdOutTask();
                        mdOutTask.setOutTaskId(mdOutTaskMap.getOutTaskId());
                        mdOutTask.setStatusCode("2");
                        mdOutTask.setOperationDate(DateUtils.getNowDate());
                        mdOutTaskMapper.updateMdOutTask(mdOutTask);

                        if ("2".equals(mdOutTaskMap.getTaskOrderType())) {
                            IotaskNo[0] = mdOutTaskMap.getOutTaskNo();
                        }
                    }

                } catch (Exception e) {
                    // 发生异常时，回滚事务
                    status.setRollbackOnly();
                    throw e;
                }
                return null;
            }
        });

        if ("2".equals(mdOutTaskMap.getTaskOrderType())) {
            int i = insetMdIoDetInfo(mdOutTaskMap);
            if (i == 1) {
                //将出入库设备结果信息插入到中间数据库，同时通知计量服务总线从中间数据库中读取出入库结果信息
                client.sibSendIOInfo(mdOutTaskMap.getOperId().toString());
                if (!"".equals(IotaskNo[0])) {
                    client.sibUpIOResult(IotaskNo[0], "2");
                }
            }
        }
        return 1;
    }

    public int insetMdIoDetInfo(MdOutTaskDetailsController.MdOutTaskMap mdOutTaskMap) {
        String assetsCategory = mdOutTaskMap.getAssetsCategory();

        List<MdOutTaskDetails> mdOutTaskDetailsList = mdOutTaskMap.getMdOutTaskDetailsList();
        for (MdOutTaskDetails mdOutTaskDetails : mdOutTaskDetailsList) {
            //出库设备结果信息插入到中间数据库
//            MdIoDet mdIoDet = new MdIoDet();
//            mdIoDet.setDetId((long) 1);
//            mdIoDet.setIoTaskNo(mdOutTaskDetails.getOutTaskNo());
//            mdIoDet.setRelaId(mdOutTaskMap.getRelaId().toString());
//            //mdIoDet.setStoreLocBar();
//            mdIoDet.setEquipId(mdOutTaskDetails.getEquipId());
//            mdIoDet.setBarCode(mdOutTaskDetails.getRfidNo());
//            mdIoDet.setMadeNo(mdOutTaskDetails.getMadeNo());
//            //设备类别，对应系统  01电能表03互感器09终端
//            mdIoDet.setEquipCateg(assetsCategory);
//            mdIoDet.setWhId(mdOutTaskDetails.getWhId());
//            mdIoDet.setWhAreaId(mdOutTaskDetails.getWhAreaId());
//            mdIoDet.setStoreAreaId(mdOutTaskDetails.getStoreAreaId());
//            mdIoDet.setStoreLocId(mdOutTaskDetails.getStoreLocId());
//            mdIoDet.setOperId(mdOutTaskMap.getOperId());
//
//            mdIoDetMapper.insertMdIoDet(mdIoDet);
        }
        return 1;
    }

    /**
     * 批量删除出入库明细
     *
     * @param outTaskDetailsIds 需要删除的出入库明细主键
     * @param assetsCategory    任务类别
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteMdOutTaskDetailsByOutTaskDetailsIds(Long[] outTaskDetailsIds, String assetsCategory) {
        for (Long outTaskDetailsId : outTaskDetailsIds) {
            MdOutTaskDetails mdOutTaskDetails = mdOutTaskDetailsMapper.selectMdOutTaskDetailsByOutTaskDetailsId(outTaskDetailsId);
            this.updateServiceStatus(assetsCategory, mdOutTaskDetails.getAssetsId(), "01");
        }
        return mdOutTaskDetailsMapper.deleteMdOutTaskDetailsByOutTaskDetailsIds(outTaskDetailsIds);
    }

    /**
     * 删除出入库明细信息
     *
     * @param outTaskDetailsId 出入库明细主键
     * @return 结果
     */
    @Override
    public int deleteMdOutTaskDetailsByOutTaskDetailsId(Long outTaskDetailsId) {
        return mdOutTaskDetailsMapper.deleteMdOutTaskDetailsByOutTaskDetailsId(outTaskDetailsId);
    }

    /**
     * 导入入库明细数据
     *
     * @param outTaskDetailsList 入库明细列表
     * @param outTaskNo          任务编号
     * @return 结果
     */
    @Transactional
    public String importMdOutTaskDetails(List<MdOutTaskDetails> outTaskDetailsList, String outTaskNo, String assetsCategory, Boolean isCoverSupport, MdOutTask mdOutTask) {
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        //如果覆盖先删除
        if (isCoverSupport) {
            MdOutTaskDetails mdOutTaskDetailsQuery = new MdOutTaskDetails();
            mdOutTaskDetailsQuery.setOutTaskNo(outTaskNo);
            List<MdOutTaskDetails> mdOutTaskDetailsList = mdOutTaskDetailsMapper.selectMdOutTaskDetailsList(mdOutTaskDetailsQuery);

            for (MdOutTaskDetails mdOutTaskDetails : mdOutTaskDetailsList) {
                //返回状态
                this.updateServiceStatus(assetsCategory, mdOutTaskDetails.getAssetsId(), "01");
            }

            //删除原来的明细
            mdOutTaskDetailsMapper.deleteMdOutTaskDetailsByOutTaskNo(outTaskNo);
        }
        for (MdOutTaskDetails mdOutTaskDetails : outTaskDetailsList) {
            //是否操作出库
            boolean isOperation = true;

            String rfidNo = mdOutTaskDetails.getRfidNo();
            try {
                //对象验证
                BeanValidators.validateWithException(validator, mdOutTaskDetails);

                switch (assetsCategory) {
                    case "01":
                        MdMeter mdMeter = mdMeterMapper.selectMdMeterByRfidNo(rfidNo);

                        if (StringUtils.isNull(mdMeter)) {
                            failureNum++;
                            failureMsg.append("<br/>" + failureNum + "、条形码 " + rfidNo + " 的电表信息在台账中不存在");
                            isOperation = false;
                        } else if (!"01".equals(mdMeter.getServiceStatus())) {
                            failureNum++;
                            failureMsg.append("<br/>" + failureNum + "、条形码 " + rfidNo + " 的电表不是正常在库状态，不能导入明细");
                            isOperation = false;
                        } else {
                            mdOutTaskDetails.setAssetsId(mdMeter.getMeterId());
                        }

                        if (isOperation) {
                            //记录位置
                            mdOutTaskDetails.setWhId(mdMeter.getWhId());
                            mdOutTaskDetails.setWhAreaId(mdMeter.getWhAreaId());
                            mdOutTaskDetails.setStoreAreaId(mdMeter.getStoreAreaId());
                            mdOutTaskDetails.setStoreLocId(mdMeter.getStoreLocId());

                            //执行修改
                            MdMeter mdMeterUpdate = new MdMeter();
                            mdMeterUpdate.setMeterId(mdMeter.getMeterId());
                            mdMeterUpdate.setServiceStatus("02");
                            mdMeterMapper.updateMdMeter(mdMeterUpdate);
                        }
                        break;
                    case "02":
                        MdIt mdIt = mdItMapper.selectMdItByRfidNo(rfidNo);
                        if (StringUtils.isNull(mdIt)) {
                            failureNum++;
                            failureMsg.append("<br/>" + failureNum + "、条形码 " + rfidNo + " 的互感器信息在台账中不存在");
                            isOperation = false;
                        } else if (!"01".equals(mdIt.getServiceStatus())) {
                            failureNum++;
                            failureMsg.append("<br/>" + failureNum + "、条形码 " + rfidNo + " 的互感器不是正常在库状态，不能导入明细");
                            isOperation = false;
                        } else {
                            mdOutTaskDetails.setAssetsId(mdIt.getItId());
                        }

                        if (isOperation) {
                            //记录位置
                            mdOutTaskDetails.setWhId(mdIt.getWhId());
                            mdOutTaskDetails.setWhAreaId(mdIt.getWhAreaId());
                            mdOutTaskDetails.setStoreAreaId(mdIt.getStoreAreaId());
                            mdOutTaskDetails.setStoreLocId(mdIt.getStoreLocId());

                            //执行修改
                            MdIt mdItUpdate = new MdIt();
                            mdItUpdate.setItId(mdIt.getItId());
                            mdItUpdate.setServiceStatus("02");
                            mdItMapper.updateMdIt(mdItUpdate);
                        }
                        break;
                    case "03":
                        MdLcEquip mdLcEquip = mdLcEquipMapper.selectMdLcEquipByRfidNo(rfidNo);
                        if (StringUtils.isNull(mdLcEquip)) {
                            failureNum++;
                            failureMsg.append("<br/>" + failureNum + "、条形码 " + rfidNo + " 的终端信息在台账中不存在");
                            isOperation = false;
                        } else if (!"01".equals(mdLcEquip.getServiceStatus())) {
                            failureNum++;
                            failureMsg.append("<br/>" + failureNum + "、条形码 " + rfidNo + " 的终端不是正常在库状态，不能导入明细");
                            isOperation = false;
                        } else {
                            mdOutTaskDetails.setAssetsId(mdLcEquip.getLcEquipId());
                        }

                        if (isOperation) {
                            //记录位置
                            mdOutTaskDetails.setWhId(mdLcEquip.getWhId());
                            mdOutTaskDetails.setWhAreaId(mdLcEquip.getWhAreaId());
                            mdOutTaskDetails.setStoreAreaId(mdLcEquip.getStoreAreaId());
                            mdOutTaskDetails.setStoreLocId(mdLcEquip.getStoreLocId());

                            //执行修改
                            MdLcEquip mdLcEquipUpdate = new MdLcEquip();
                            mdLcEquipUpdate.setLcEquipId(mdLcEquip.getLcEquipId());
                            mdLcEquipUpdate.setServiceStatus("02");
                            mdLcEquipMapper.updateMdLcEquip(mdLcEquipUpdate);
                        }
                        break;
                    case "04":
                        MdConcentrator mdConcentrator = mdConcentratorMapper.selectMdConcentratorByRfidNo(rfidNo);
                        if (StringUtils.isNull(mdConcentrator)) {
                            failureNum++;
                            failureMsg.append("<br/>" + failureNum + "、条形码 " + rfidNo + " 的集中器信息在台账中不存在");
                            isOperation = false;
                        } else if (!"01".equals(mdConcentrator.getServiceStatus())) {
                            failureNum++;
                            failureMsg.append("<br/>" + failureNum + "、条形码 " + rfidNo + " 的集中器不是正常在库状态，不能导入明细");
                            isOperation = false;
                        } else {
                            mdOutTaskDetails.setAssetsId(mdConcentrator.getConcentratorId());
                        }

                        if (isOperation) {
                            //记录位置
                            mdOutTaskDetails.setWhId(mdConcentrator.getWhId());
                            mdOutTaskDetails.setWhAreaId(mdConcentrator.getWhAreaId());
                            mdOutTaskDetails.setStoreAreaId(mdConcentrator.getStoreAreaId());
                            mdOutTaskDetails.setStoreLocId(mdConcentrator.getStoreLocId());

                            //执行修改
                            MdConcentrator mdConcentratorUpdate = new MdConcentrator();
                            mdConcentratorUpdate.setConcentratorId(mdConcentrator.getConcentratorId());
                            mdConcentratorUpdate.setServiceStatus("02");
                            mdConcentratorMapper.updateMdConcentrator(mdConcentratorUpdate);
                        }
                        break;
                    case "05":
                        MdComModule mdComModule = mdComModuleMapper.selectMdComModuleByRfidNo(rfidNo);
                        if (StringUtils.isNull(mdComModule)) {
                            failureNum++;
                            failureMsg.append("<br/>" + failureNum + "、条形码 " + rfidNo + " 的通讯模块信息在台账中不存在");
                            isOperation = false;
                        } else if (!"01".equals(mdComModule.getServiceStatus())) {
                            failureNum++;
                            failureMsg.append("<br/>" + failureNum + "、条形码 " + rfidNo + " 的通讯模块不是正常在库状态，不能导入明细");
                            isOperation = false;
                        } else {
                            mdOutTaskDetails.setAssetsId(mdComModule.getModuleId());
                        }

                        if (isOperation) {
                            //记录位置
                            mdOutTaskDetails.setWhId(mdComModule.getWhId());
                            mdOutTaskDetails.setWhAreaId(mdComModule.getWhAreaId());
                            mdOutTaskDetails.setStoreAreaId(mdComModule.getStoreAreaId());
                            mdOutTaskDetails.setStoreLocId(mdComModule.getStoreLocId());

                            //执行修改
                            MdComModule mdComModuleUpdate = new MdComModule();
                            mdComModuleUpdate.setModuleId(mdComModule.getModuleId());
                            mdComModuleUpdate.setServiceStatus("02");
                            mdComModuleMapper.updateMdComModule(mdComModuleUpdate);
                        }
                        break;
                    case "10":
                        MdOtherMaterial mdOtherMaterial = mdOtherMaterialMapper.selectMdOtherMaterialByBarCode(rfidNo);
                        if (StringUtils.isNull(mdOtherMaterial)) {
                            failureNum++;
                            failureMsg.append("<br/>" + failureNum + "、条形码 " + rfidNo + " 的其他物资信息在台账中不存在");
                            isOperation = false;
                        } else if (!"01".equals(mdOtherMaterial.getServiceStatus())) {
                            failureNum++;
                            failureMsg.append("<br/>" + failureNum + "、条形码 " + rfidNo + " 的其他物资不是正常在库状态，不能导入明细");
                            isOperation = false;
                        } else {
                            mdOutTaskDetails.setAssetsId(mdOtherMaterial.getOtherMaterialId());
                        }
                        if (isOperation) {
                            //记录位置
                            mdOutTaskDetails.setWhId(mdOtherMaterial.getWhId());
                            mdOutTaskDetails.setWhAreaId(mdOtherMaterial.getWhAreaId());
                            mdOutTaskDetails.setStoreAreaId(mdOtherMaterial.getStoreAreaId());
                            mdOutTaskDetails.setStoreLocId(mdOtherMaterial.getStoreLocId());

                            //执行修改
                            MdOtherMaterial mdOtherMaterialUpdate = new MdOtherMaterial();
                            mdOtherMaterialUpdate.setOtherMaterialId(mdOtherMaterial.getOtherMaterialId());
                            mdOtherMaterialUpdate.setServiceStatus("02");
                            mdOtherMaterialMapper.updateMdOtherMaterial(mdOtherMaterialUpdate);
                        }
                        break;
                }

//                MdOutTaskDetails mdOutTaskDetailsQuery = new MdOutTaskDetails();
//                mdOutTaskDetailsQuery.setRfidNo(mdOutTaskDetails.getRfidNo());
//                MdOutTaskDetails mdOutTaskDetailsTemp = mdOutTaskDetailsMapper.selectCarryMdOutTaskDetailsCount(mdOutTaskDetailsQuery);
//                if (StringUtils.isNotNull(mdOutTaskDetailsTemp)) {
//                    if (mdOutTaskDetailsTemp.getRepeatNumber() > 0) {
//                        failureMsg.append("<br/>" + failureNum + "、条形码 " + rfidNo + " 的信息在" + mdOutTaskDetailsTemp.getOutTaskNo() + "的任务中已存在");
//                        isOperation = false;
//                    }
//                }

                //执行插入
                if (isOperation) {
                    mdOutTaskDetails.setOutTaskNo(outTaskNo);
                    mdOutTaskDetailsMapper.insertMdOutTaskDetails(mdOutTaskDetails);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、条形码 " + rfidNo + " 的明细导入成功");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、条形码 " + rfidNo + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
                throw new ServiceException(failureMsg.toString());
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    //判断参数是否一直
    private Boolean compareMeterParams(MdOutTask mdOutTask, MdMeter mdMeter) {
        String voltCode = mdOutTask.getVoltCode();
        String currentCode = mdOutTask.getCurrentCode();
        String apPreLevelCode = mdOutTask.getApPreLevelCode();
        String wiringMode = mdOutTask.getWiringMode();

        String voltCodeMd = mdMeter.getVoltCode();
        String currentCodeMd = mdMeter.getCurrentCode();
        String apPreLevelCodeMd = mdMeter.getApPreLevelCode();
        String wiringModeMd = mdMeter.getWiringMode();

        if (StringUtils.isNotBlank(voltCode) && !StringUtils.equals(voltCode, voltCodeMd)) {
            return false;
        }
        if (StringUtils.isNotBlank(currentCode) && !StringUtils.equals(currentCode, currentCodeMd)) {
            return false;
        }
        if (StringUtils.isNotBlank(apPreLevelCode) && !StringUtils.equals(apPreLevelCode, apPreLevelCodeMd)) {
            return false;
        }
        if (StringUtils.isNotBlank(wiringMode) && !StringUtils.equals(wiringMode, wiringModeMd)) {
            return false;
        }

        return true;
    }

    //修改各大资产业务状态
    public int updateServiceStatus(String assetsCategory, Long assetsId, String serviceStatus) {
        switch (assetsCategory) {
            case "01":
                //退回状态
                MdMeter mdMeterUpdate = new MdMeter();
                mdMeterUpdate.setMeterId(assetsId);
                mdMeterUpdate.setServiceStatus(serviceStatus);
                mdMeterMapper.updateMdMeter(mdMeterUpdate);
                break;
            case "02":
                //退回状态
                MdIt mdItUpdate = new MdIt();
                mdItUpdate.setItId(assetsId);
                mdItUpdate.setServiceStatus(serviceStatus);
                mdItMapper.updateMdIt(mdItUpdate);
                break;
            case "03":
                //退回状态
                MdLcEquip mdLcEquipUpdate = new MdLcEquip();
                mdLcEquipUpdate.setLcEquipId(assetsId);
                mdLcEquipUpdate.setServiceStatus(serviceStatus);
                mdLcEquipMapper.updateMdLcEquip(mdLcEquipUpdate);
                break;
            case "04":
                //退回状态
                MdConcentrator mdConcentratorUpdate = new MdConcentrator();
                mdConcentratorUpdate.setConcentratorId(assetsId);
                mdConcentratorUpdate.setServiceStatus(serviceStatus);
                mdConcentratorMapper.updateMdConcentrator(mdConcentratorUpdate);
                break;
            case "05":
                //退回状态
                MdComModule mdComModuleUpdate = new MdComModule();
                mdComModuleUpdate.setModuleId(assetsId);
                mdComModuleUpdate.setServiceStatus(serviceStatus);
                mdComModuleMapper.updateMdComModule(mdComModuleUpdate);
                break;
            case "10":
                //退回状态
                MdOtherMaterial mdOtherMaterialUpdate = new MdOtherMaterial();
                mdOtherMaterialUpdate.setOtherMaterialId(assetsId);
                mdOtherMaterialUpdate.setServiceStatus(serviceStatus);
                mdOtherMaterialMapper.updateMdOtherMaterial(mdOtherMaterialUpdate);
                break;
        }
        return 1;
    }

    /**
     * 查询出库数量信息
     *
     * @return 结果
     */
    @Override
    public Map<String, Object> getOutNumInfo() {
        Map<String, Object> inNumInfo = new LinkedHashMap<>();

        MdOutTaskDetails mdOutTaskDetails = new MdOutTaskDetails();
        mdOutTaskDetails.setIsComplete("1");

        Integer thisWeekOutNum = mdOutTaskDetailsMapper.getThisWeekOutNum(mdOutTaskDetails);
        Integer lastWeekOutNum = mdOutTaskDetailsMapper.getLastWeekOutNum(mdOutTaskDetails);
        Integer thisMonthOutNum = mdOutTaskDetailsMapper.getThisMonthOutNum(mdOutTaskDetails);
        Integer lastMonthOutNum = mdOutTaskDetailsMapper.getLastMonthOutNum(mdOutTaskDetails);

        double weekChangeOut = HiultraUtil.countNumChange(thisWeekOutNum, lastWeekOutNum);
        double monthChangeOut = HiultraUtil.countNumChange(thisMonthOutNum, lastMonthOutNum);

        inNumInfo.put("本周出库数", thisWeekOutNum);
        inNumInfo.put("上周出库数", lastWeekOutNum);
        inNumInfo.put("出库周环比变化", weekChangeOut);
        inNumInfo.put("本月出库数", thisMonthOutNum);
        inNumInfo.put("上月出库数", lastMonthOutNum);
        inNumInfo.put("出库月环比变化", monthChangeOut);
        return inNumInfo;
    }

    /**
     * 查询出入库明细数量
     *
     * @param mdOutTaskDetails 出入库明细
     * @return 出入库明细数量
     */
    @Override
    public int selectMdOutTaskDetailsCount(MdOutTaskDetails mdOutTaskDetails) {
        return mdOutTaskDetailsMapper.selectMdOutTaskDetailsCount(mdOutTaskDetails);
    }
}
